package com.ruoyi.traveladmin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.traveladmin.mapper.TaTravelDetailMapper;
import com.ruoyi.traveladmin.service.ITaTravelDetailService;
import com.ruoyi.traveladmin.domain.TaTravelDetail;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.traveladmin.domain.TaTravel;
import com.ruoyi.traveladmin.service.ITaTravelService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class TaTravelDetailServiceImpl implements ITaTravelDetailService {


    @Resource
    private TaTravelDetailMapper taTravelDetailMapper;
    
    
    @Resource
    private ITaTravelService taTravelService;

    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param taTravelDetail 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<TaTravelDetail> getTaTravelDetailPage(PageDomain pageDomain, TaTravelDetail taTravelDetail , TaTravel taTravel) {
        if (Objects.isNull(taTravelDetail)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<TaTravelDetail> queryWrapper = new LambdaQueryWrapper<>(new TaTravelDetail());
            //if(!Objects.isNull(taTravelDetail.getName())){
            //	queryWrapper.like(TaTravelDetail::getName,taTravelDetail.getName());
            //}
            

            if (!Objects.isNull(taTravelDetail.getId())) {
                queryWrapper.eq(TaTravelDetail::getId, taTravelDetail.getId());
            }

            if (!Objects.isNull(taTravelDetail.getTaTravelId())) {
                queryWrapper.eq(TaTravelDetail::getTaTravelId, taTravelDetail.getTaTravelId());
            }

            taTravel.setBaseEntityNull();
            taTravel.setId(null);
            if (!PageDomain.checkObjAllFieldsIsNull(taTravel)) {
                List<Long> idList = taTravelService.getIdListByTaTravel(taTravel);
                queryWrapper.in(TaTravelDetail::getTaTravelId, idList);
            }


            if (!Objects.isNull(taTravelDetail.getDetailText())) {
                queryWrapper.like(TaTravelDetail::getDetailText, taTravelDetail.getDetailText());
            }

            if (!Objects.isNull(taTravelDetail.getStatus())) {
                queryWrapper.like(TaTravelDetail::getStatus, taTravelDetail.getStatus());
            }

            if (!Objects.isNull(taTravelDetail.getDelFlag())) {
                queryWrapper.like(TaTravelDetail::getDelFlag, taTravelDetail.getDelFlag());
            }

            if (!Objects.isNull(taTravelDetail.getCreateBy())) {
                queryWrapper.like(TaTravelDetail::getCreateBy, taTravelDetail.getCreateBy());
            }

            if (!Objects.isNull(taTravelDetail.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(taTravelDetail.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(taTravelDetail.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(TaTravelDetail::getCreateTime, taTravelDetail.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(taTravelDetail.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(TaTravelDetail::getCreateTime, taTravelDetail.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(taTravelDetail.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(taTravelDetail.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(TaTravelDetail::getCreateTime, taTravelDetail.getParams().get("CreateTimeBeginTime"), taTravelDetail.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(taTravelDetail.getUpdateBy())) {
                queryWrapper.like(TaTravelDetail::getUpdateBy, taTravelDetail.getUpdateBy());
            }

            if (!Objects.isNull(taTravelDetail.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(taTravelDetail.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(taTravelDetail.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(TaTravelDetail::getUpdateTime, taTravelDetail.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(taTravelDetail.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(TaTravelDetail::getUpdateTime, taTravelDetail.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(taTravelDetail.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(taTravelDetail.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(TaTravelDetail::getUpdateTime, taTravelDetail.getParams().get("UpdateTimeBeginTime"), taTravelDetail.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(taTravelDetail.getRemark())) {
                queryWrapper.like(TaTravelDetail::getRemark, taTravelDetail.getRemark());
            }
        Page<TaTravelDetail> taTravelDetailList= taTravelDetailMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(TaTravelDetail record:taTravelDetailList.getRecords()){
            record.setTaTravel(taTravelService.getTaTravelById(record.getTaTravelId()));
        }

            return taTravelDetailList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public TaTravelDetail getTaTravelDetailById(Long id) {
        if (Objects.isNull(id)) {
            return new TaTravelDetail();
        }
        try {
            TaTravelDetail taTravelDetail = taTravelDetailMapper.selectById(id);
            taTravelDetail.setTaTravel(taTravelService.getTaTravelById(taTravelDetail.getTaTravelId()));
            if (Objects.isNull(taTravelDetail)){
                taTravelDetail = new TaTravelDetail();
            }
            return taTravelDetail;

        } catch (Exception e) {
            e.printStackTrace();
            return new TaTravelDetail();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param taTravelDetail 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByTaTravelDetail(TaTravelDetail taTravelDetail) {
        if (Objects.isNull(taTravelDetail)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<TaTravelDetail> queryWrapper = new LambdaQueryWrapper<>(new TaTravelDetail());
            

            if (!Objects.isNull(taTravelDetail.getId())) {
                queryWrapper.eq(TaTravelDetail::getId, taTravelDetail.getId());
            }

            if (!Objects.isNull(taTravelDetail.getTaTravelId())) {
                queryWrapper.eq(TaTravelDetail::getTaTravelId, taTravelDetail.getTaTravelId());
            }

            if (!Objects.isNull(taTravelDetail.getDetailText())) {
                queryWrapper.like(TaTravelDetail::getDetailText, taTravelDetail.getDetailText());
            }

            if (!Objects.isNull(taTravelDetail.getStatus())) {
                queryWrapper.like(TaTravelDetail::getStatus, taTravelDetail.getStatus());
            }

            if (!Objects.isNull(taTravelDetail.getDelFlag())) {
                queryWrapper.like(TaTravelDetail::getDelFlag, taTravelDetail.getDelFlag());
            }

            if (!Objects.isNull(taTravelDetail.getCreateBy())) {
                queryWrapper.like(TaTravelDetail::getCreateBy, taTravelDetail.getCreateBy());
            }

            if (!Objects.isNull(taTravelDetail.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(taTravelDetail.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(taTravelDetail.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(TaTravelDetail::getCreateTime, taTravelDetail.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(taTravelDetail.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(TaTravelDetail::getCreateTime, taTravelDetail.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(taTravelDetail.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(taTravelDetail.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(TaTravelDetail::getCreateTime, taTravelDetail.getParams().get("CreateTimeBeginTime"), taTravelDetail.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(taTravelDetail.getUpdateBy())) {
                queryWrapper.like(TaTravelDetail::getUpdateBy, taTravelDetail.getUpdateBy());
            }

            if (!Objects.isNull(taTravelDetail.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(taTravelDetail.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(taTravelDetail.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(TaTravelDetail::getUpdateTime, taTravelDetail.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(taTravelDetail.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(TaTravelDetail::getUpdateTime, taTravelDetail.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(taTravelDetail.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(taTravelDetail.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(TaTravelDetail::getUpdateTime, taTravelDetail.getParams().get("UpdateTimeBeginTime"), taTravelDetail.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(taTravelDetail.getRemark())) {
                queryWrapper.like(TaTravelDetail::getRemark, taTravelDetail.getRemark());
            }

            List<TaTravelDetail> taTravelDetails = taTravelDetailMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (TaTravelDetail taTravelDetailNo:taTravelDetails){
                idList.add(taTravelDetailNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 通过taTravel外键idList查询idList
     *
     * @param taTravelIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByTaTravelIdList(List<Long> taTravelIdList) {
        if (Objects.isNull(taTravelIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<TaTravelDetail> queryWrapper = new LambdaQueryWrapper<>(new TaTravelDetail());
            queryWrapper.in(TaTravelDetail::getTaTravelId, taTravelIdList);
            List<TaTravelDetail> taTravelDetails = taTravelDetailMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (TaTravelDetail taTravelDetail:taTravelDetails){
                idList.add(taTravelDetail.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param taTravelDetail 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertTaTravelDetail(TaTravelDetail taTravelDetail) {
        if (Objects.isNull(taTravelDetail)) {
            return -1;
        }
        try {
        
            //taTravelDetail.setSpell(PinYin.getPinYin(taTravelDetail.getName()));
            return taTravelDetailMapper.insert(taTravelDetail);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param taTravelDetail 实体类
     * @return 结果
     */
    @Override
    public int updateTaTravelDetailById(TaTravelDetail taTravelDetail) {
        if (Objects.isNull(taTravelDetail)) {
            return -1;
        }
        try {
            //taTravelDetail.setSpell(PinYin.getPinYin(taTravelDetail.getName()));
            return taTravelDetailMapper.updateById(taTravelDetail);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteTaTravelDetailByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return taTravelDetailMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<TaTravelDetail> getTaTravelDetailList() {
        try {
            LambdaQueryWrapper<TaTravelDetail> queryWrapper = new LambdaQueryWrapper<>(new TaTravelDetail());
            queryWrapper.isNotNull(TaTravelDetail::getId);
            return taTravelDetailMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
